<template>
  <div>
    <v-header title="地址管理">
      <v-back></v-back>
    </v-header>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    <van-button
      id="del"
      type="primary"
      block
      color="#ccc"
      round
      @click="delAdderss"
      >删除地址</van-button
    >
  </div>
</template>

<script>
import { Dialog } from "vant";
import { addressList, addressDel } from "@/request/api.js";
export default {
  data() {
    return {
      type: "edit",
      uid: "",
      chosenAddressId: 0,
      list: [],
      edit: [],
      ind:''
    };
  },
  async created() {
    this.uid = JSON.parse(localStorage.getItem("userInfo")).uid;
    let res = await addressList(this.uid);
    res.data.list.forEach((item) => {
      this.list.push({
        id: item.id.toString(),
        name: item.username,
        tel: item.userphone,
        address: item.location + " " + item.useraddress,
      });
    });
    this.edit = res.data.list;
  },
  methods: {
    onAdd() {
      this.$router.push("/add_address");
    },
    delAdderss() {
      if (this.chosenAddressId) {
        Dialog.confirm({
          title: "提示",
          message: "确定要删除吗？？",
        }).then(async () => {
          let res = await addressDel(this.chosenAddressId);
          location.reload();
        });
      }
    },
    onEdit(item, index) {
        this.$router.push({
            path:'/add_address',
            query:{
                type:'edit',
                ...this.edit[index]
            }
        });
    },
  },
};
</script>
<style scoped>
#cho{
  margin-bottom: 20px;
}
</style>